{extend name='layout:app'}

{block name='meta'}
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
{/block}

{block name='css'}
<link rel="stylesheet" href="__STATIC__/css/addr-edit.css">
<link rel="stylesheet" href="__CSS__/jquery-weui.min.css">
<script src="__JS__/jQuery-weui.min.js"></script>
<script src="__JS__/city-picker.min.js"></script>
{/block}
{block name='content'}
<div id="app">
    <div class="nav">
        <i class="el-icon-arrow-left icon_left" @click="back"></i>
        <div>添加地址</div>
        <i class="iconfont icon-home icon_right" @click="home"></i>
    </div>

    <div class="edit-area">
        <div class="edit-item">
            <el-input
                    placeholder="请输入姓名"
                    prefix-icon="iconfont icon-user-"
                    v-model="name" clearable>
            </el-input>
        </div>
        <div class="edit-item">
            <el-input
                    placeholder="请输入手机号"
                    prefix-icon="iconfont icon-phone"
                    v-model="phone" clearable>
            </el-input>
        </div>
        <div class="edit-item set-city-picker">
            <el-input
                    placeholder="请选择省市区/县"
                    prefix-icon="iconfont icon-building"
                    suffix-icon="el-icon-arrow-right"
                    v-model="addr">
            </el-input>
        </div>
        <div class="edit-item">
            <el-input
                    placeholder="详细地址（如学校、宿舍楼、楼层、门牌号）"
                    prefix-icon="iconfont icon-address"
                    v-model="detailAddr" clearable>
            </el-input>
        </div>
    </div>


    <div class="btn-area">
        <div class="order">
            <el-button type="primary" @click="add">确认添加</el-button>
        </div>
    </div>
</div>
{/block}

{block name='footer'}{/block}

{block name='js'}
<script>
    var that;
    new Vue({
        el:'#app',
        data: {
            name: '',
            phone: '',
            addr: '',
            detailAddr: ''
        },
        created() {
            that = this
            setTimeout(() => {
                $('.set-city-picker').find('input').attr('id', 'city-picker')
                $("#city-picker").cityPicker({
                    title: "选择省市区/县",
                    onChange: function (picker, values, displayValues) {
                        console.log(values, displayValues);
                        that.addr = `${displayValues[0]} ${displayValues[1]} ${displayValues[2]}`
                    }
                });
                console.log(sessionStorage.getItem('clickType'))
            }, 50)
            // $("#city-picker").picker("setValue", ["110000", "110000", "110108"]);

        },
        mounted() {

        },
        methods: {
            back() {
                window.history.go(-1)
            },
            home() {
                location.href = '/'
            },
            add() {
                var _noInputCount = 0
                for(var i=0;i<$('.edit-item input').length;i++) {
                    if(!$('.edit-item input').eq(i).val()) {
                        _noInputCount++
                    }
                }
                if(_noInputCount > 0) {
                    alert('请完善信息！')
                    return
                }
                var _checkPhone = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
                if(!_checkPhone.test(this.phone)) {
                    alert('请输入正确格式的手机号码！')
                    return
                }
                $.post('/index/person/addrsave',{
                    name: this.name,
                    phone: this.phone,
                    addr: this.addr,
                    detail_addr: this.detailAddr,
                    add_type: sessionStorage.getItem('clickType') == 1 ? 1 : 0
                },function (res) {
                    if(res.code === 0) {

                        /*var uInfo = {
                            name: this.name,
                            phone: this.phone,
                            addr: this.addr+' '+this.detailAddr
                        }
                        sessionStorage.setItem('uInfo', JSON.stringify(uInfo))*/
                            // location.href = '/index/services/send.html'

                        // const _clickType = sessionStorage.getItem('clickType')
                        // if(_clickType == 0 || _clickType == 2) {
                        //     location.href = '/index/person/address?type=0'
                        // }else {
                        //     location.href = '/index/person/address?type=1'
                        // }
                        const _clickType = sessionStorage.getItem('clickType')
                        const _service = sessionStorage.getItem('service')
                        if(_service == 'onsite') {
                            location.href = '/index/services/onsiteService'
                            return
                        }
                        if(sessionStorage.getItem('send-type') == 1) {
                            location.href = '/index/services/send?send_way=1'
                        }else {
                            location.href = '/index/services/send'
                        }
                    }
                },'json')
                /*var uInfo = {
                    name: this.name,
                    phone: this.phone,
                    addr: this.addr+' '+this.detailAddr,
                    // type: sessionStorage.getItem('clickType')
                }
                if(document.referrer.includes('onsite-service') || document.referrer.includes('addr-list3')) {
                    localStorage.setItem('uInfo3', JSON.stringify(uInfo))
                    location.href = 'addr-list3.html'
                    return
                }
                //send-by2
                if(location.href.includes('?comefrom=sendBy2&type=0')) {
                    localStorage.setItem('uInfo', JSON.stringify(uInfo))
                    location.href = 'addr-list.html?comefrom=sendBy2'
                    return
                }else if(location.href.includes('?comefrom=sendBy2&type=1')) {
                    localStorage.setItem('uInfo2', JSON.stringify(uInfo))
                    location.href = 'addr-list2.html?comefrom=sendBy2'
                    return
                }

                if(sessionStorage.getItem('clickType') == 1 || document.referrer.indexOf('addr-list2') != -1) {
                    localStorage.setItem('uInfo2', JSON.stringify(uInfo))
                    // $.post('')
                    location.href = 'addr-list2.html'
                }else {
                    localStorage.setItem('uInfo', JSON.stringify(uInfo))
                    location.href = 'addr-list.html'
                }*/
            }
        }
    });





</script>
{/block}